# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=zfs
pkgver=2.2.4
pkgrel=0
pkgdesc="Advanced filesystem and volume manager"
url="https://openzfs.org"
arch="all"
license="CDDL-1.0"
provides="spl=$pkgver-r$pkgrel" # Mitigate upgrade conflicts
depends_dev="
	attr-dev
	e2fsprogs-dev
	glib-dev
	libtirpc-dev
	openssl-dev>3
	"
makedepends="
	$depends_dev
	linux-headers
	py3-cffi
	py3-distlib
	py3-setuptools
	python3-dev
	util-linux-dev
	"
options="!check" # need to be run on live system w/ ZFS loaded.
subpackages="
	$pkgname-bash-completion
	$pkgname-dev
	$pkgname-doc
	$pkgname-dracut::noarch
	$pkgname-libs
	$pkgname-openrc
	$pkgname-scripts
	$pkgname-udev
	$pkgname-utils-py:utils_py:noarch
	py3-pyzfs-pyc
	py3-pyzfs:pyzfs:noarch
	"
source="https://github.com/openzfs/zfs/releases/download/zfs-$pkgver/zfs-$pkgver.tar.gz
	alpine-bash-completion-dir.patch
	"

# secfixes:
#   2.2.1-r1:
#     - CVE-2023-49298

build() {
	export CFLAGS="$CFLAGS -fno-tree-vectorize"
	export CXXFLAGS="$CXXFLAGS -fno-tree-vectorize"
	export LIBS="$LIBS -lintl"
	./configure --prefix=/usr \
		--with-tirpc \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--with-config=user \
		--with-udevdir=/lib/udev \
		--disable-systemd \
		--disable-static \
		--with-python=3 \
		--enable-pyzfs
	make
}

package() {
	make DESTDIR="$pkgdir" install

	# no initramfs-tools
	rm -rf "$pkgdir"/usr/share/initramfs-tools

	# we use doas(1)
	rm -rf "$pkgdir"/etc/sudoers.d

	# zfs-mount-generator is a systemd.generator(7)
	rm "$pkgdir"/usr/share/man/man8/zfs-mount-generator*

	# Fix permissions of zfs OpenRC service configuration (#13840)
	chmod 644 "$pkgdir"/etc/conf.d/zfs
}

dracut() {
	pkgdesc="$pkgdesc (dracut)"
	install_if="$pkgname=$pkgver-r$pkgrel dracut-modules"

	amove usr/lib/dracut
}

udev() {
	pkgdesc="$pkgdesc (udev)"

	amove lib/udev
}

scripts() {
	pkgdesc="$pkgdesc (scripts)"

	amove usr/share/zfs
}

utils_py() {
	pkgdesc="$pkgdesc (python utils)"
	depends="python3"

	amove usr/bin/arc_summary
	amove usr/bin/arcstat
	amove usr/bin/dbufstat
	amove usr/bin/zilstat
}

pyzfs() {
	pkgdesc="$pkgdesc (Python lib to interact with ZFS)"
	depends="python3 $pkgname"

	amove usr/lib/python3*
}

sha512sums="
1d17e30573d594fb5c9ea77cde104616dca362fed7530296816d1b55173594f66170fcfb23ab57c27074f85b79d3eb557b4ee9a1c420e507b2434a7902d8dcc1  zfs-2.2.4.tar.gz
f662f763b5d220525c0a9eada6a643694987ae3741fbf4d86aa4a557a011ee70e833e3e9c67bf14c3c2b5856395557b1ec1943e8620305ebc50163cffd83f43d  alpine-bash-completion-dir.patch
"
